/*========= Starting function =========*/
$(function(){

	/*========= Popover =========*/
    $('a[rel="popover"]').popover();

    
    /*========= Responsive navigation menu and dropdown =========*/
	$('.respNav a').click(function () {
		var respNav = $('.responsiveNav');
		if(respNav.is(":hidden")) {
			respNav.slideDown();
			$("html, body").animate({scrollTop:0},"slow");
		} else {
			respNav.slideUp();
		}
		return false;
    });

    $('.responsiveNav li a').click(function () {
		var subNav = $(this).parent().find('ul');
		$('.responsiveNav li ul').slideUp();
		$('.responsiveNav li').removeClass('active');
		if(subNav.is(":hidden")) {
			subNav.slideDown();
			subNav.parent().addClass('active')
		} else {
			subNav.slideUp();
			subNav.parent().removeClass('active')
		}
    });

    $(document).ready(function() {
		var submenu = $('.responsiveNav li').find('ul');
		if (submenu.length > 0) {
			submenu.parent().children('a').click(function() {
				return false;
			});
		}
	});

	/*========= Sidebar menu and dropdown functions =========*/
	$('ul.navigation > li > a').click(function(){
		var submenu = $(this).parent().find('ul.subMenu');
		$("ul.navigation li:not(.active)").children('ul.subMenu').slideUp(350);
		$('ul.navigation li').removeClass('dropActive');
		$("ul.navigation li:not(.active) a").find('span.collapse').removeClass('collapse').addClass('expand');
		if(submenu.is(":hidden")) {
			submenu.slideDown(350);
			submenu.parent().find('span.expand').removeClass('expand').addClass('collapse');
			submenu.parent().addClass('dropActive');
		} else {
			submenu.slideUp(350);
			submenu.parent().find('span.collapse').removeClass('collapse').addClass('expand');
			submenu.parent().removeClass('dropActive');
		}
	});

	$(document).ready(function() {
		var submenu = $('ul.navigation li').find('ul.subMenu');
		var active = $('ul.navigation li.active');
		if (submenu.length > 0) {
			submenu.parent('li').children('a').append('<span class="expand"></span>');
			active.addClass('dropActive');
			active.find('span.expand').removeClass('expand').addClass('collapse');
			submenu.parent().children('a').click(function() {
				return false;
			});
		}
	});

	/*========= Top navigation menu and dropdown =========*/
	$('ul.noFluidNav > li').hover(function(){
		$(this).children('ul').stop(true, true).slideDown(180);
	}, function(){
		$(this).children('ul').stop(true, true).slideUp(180);
	});

	$(document).ready(function() {
		var submenu = $('ul.noFluidNav li').find('ul');
		if (submenu.length > 0) {
			submenu.parent().children('a').click(function() {
				return false;
			});
		}
	});

	$('ul.noFluidNav > li > ul > li').hover(function(){
		$(this).children('ul').stop(true, true).slideDown(180);
	}, function(){
		$(this).children('ul').stop(true, true).slideUp(180);
	});

	$(document).ready(function() {
		var submenu = $('ul.noFluidNav li').find('ul');
		if (submenu.length > 0) {
			submenu.parent().children('a').click(function() {
				return false;
			});
		}
	});

	/*========= qtip =========*/
	$('[rel="tipsyS"]').tipsy({gravity: 's', fade: true});
	$('[rel="tipsyN"]').tipsy({gravity: 'n', fade: true});
	$('[rel="tipsyE"]').tipsy({gravity: 'e', fade: true});
	$('[rel="tipsyW"]').tipsy({gravity: 'w', fade: true});

});
/*========= End of function =========*/